//
//  LoginViewController.m
//  jang
//
//  Created by lee han on 03/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "LoginViewController.h"
#import "CRUserData.h"
#import "CommonView.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [CommonView setNativationBar:self];
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mTextPhone.delegate = self;
    _mTextPassword.delegate = self;
    
    if([CRUserData sharedInstance].m_landingPhone && [CRUserData sharedInstance].m_password){
        self.mTextPhone.text = [CRUserData sharedInstance].m_landingPhone;
        self.mTextPassword.text = [CRUserData sharedInstance].m_password;
        [self onLogin:nil];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)onLogin:(id)sender {
    [self.mTextPhone resignFirstResponder];
    [self.mTextPassword resignFirstResponder];
    
    if(self.mTextPhone.text.length != 11){
        [CommonView addAlertView:self Text:@"账号输入有误"];
        return;
    }
    
    if(self.mTextPhone.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入密码"];
        return;
    }
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    self.mTextPhone.text,  @"username",
                                    self.mTextPassword.text, @"pwd",
                                    nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_Login];
    
}

#pragma mark networkDelegate

- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if([[response objectForKey:@"status"] integerValue] == 1){
        [CRUserData sharedInstance].m_uid = [response objectForKey:@"uid"];
        [CRUserData sharedInstance].m_token = [response objectForKey:@"token"];
        [CRUserData sharedInstance].m_money = [[response objectForKey:@"money"] integerValue];
        //登陆成功
        [CRUserData sharedInstance].m_landingPhone = self.mTextPhone.text;
        [CRUserData sharedInstance].m_password = self.mTextPassword.text;
        
        [[CRUserData sharedInstance] saveLoginInfo];
        
        [[CRUserData sharedInstance] setCollectionData:[response objectForKey:@"collect"]];
        [[CRUserData sharedInstance] setShopCarData:[response objectForKey:@"shopcar"]];
        
        [[CRUserData sharedInstance] setHomeData:[response objectForKey:@"homedata"]];
        
        [CommonView gotoViewController:self Target:@"main"];
    }
    else{
        [CommonView addAlertView:self Text:@"账号密码错误"];
    }
}

//end networkDelegate

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == _mTextPhone){
        [_mTextPassword becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
    }
    return YES;
}
//end UITextFieldDelegate
@end
